﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface;
using Masuit.Tools;
using Masuit.Tools.Core.AspNetCore;

namespace RndMaterialDisposalService.Services.VehicleDispatchService
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired]
    public class VehicleMaintenanceService : IVehicleMaintenanceService
    {

        private readonly AssetsManagementContext dbContext;
        private readonly WorkflowCommonDataContext commonDataContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dbContext"></param>
        /// <param name="_commonDataContext"></param>
        public VehicleMaintenanceService(AssetsManagementContext _dbContext, WorkflowCommonDataContext _commonDataContext)
        {
            dbContext = _dbContext;
            commonDataContext = _commonDataContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputReq"></param>
        /// <returns></returns>
        public async Task<(List<VehicleDispatchReportDto> list, int count)> GetReportData(VehicleReportDataReq inputReq)
        {
            var list = dbContext.VehicleDispatchReport.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(inputReq.Subject))
            {
                list = list.Where(p => !string.IsNullOrEmpty(p.Subject) && p.Subject.Contains(inputReq.Subject));
            }
            if (!string.IsNullOrEmpty(inputReq.CarNumber))
            {
                list = list.Where(p => !string.IsNullOrEmpty(p.CarNumber) && p.CarNumber.Contains(inputReq.CarNumber));
            }
            if (!string.IsNullOrEmpty(inputReq.CarType))
            {
                list = list.Where(p => !string.IsNullOrEmpty(p.CarType) && p.CarType.Contains(inputReq.CarType));
            }
            if (!string.IsNullOrEmpty(inputReq.Applicant))
            {
                list = list.Where(p => p.Applicant == inputReq.Applicant);
            }
            if (inputReq.StartDate!=null && inputReq.EndDate!=null)
            {
                list = list.Where(p => p.ApplicationDate > inputReq.StartDate && p.ApplicationDate < inputReq.EndDate);
            }
            //处理当前节点名称
            var procinstIdList = list.Select(p => $"{p.ProcInstId}".Trim()).ToList();
            var result2 = await commonDataContext.ApplicationData.Where(p => procinstIdList.Contains(p.ProcInstId!.Trim())).ToListAsync();
            list.ForEach(item =>
            {
                var procItem = result2.Where(p => p.ProcInstId!.Trim() == item.ProcInstId!.Trim());
                if (procItem.Any())
                {
                    item.ActivityName = procItem.First().CurrentStatus?.Trim();
                }
            });

            var newlist = await list.ToListAsync();
            if (!string.IsNullOrEmpty(inputReq.ActivityName))
            {
                newlist = newlist.Where(p => p.ActivityName == inputReq.ActivityName).ToList();
            }

            //普通用户报表
            if (!string.IsNullOrEmpty(inputReq.CurrentUser))
            {
                var folioList = await commonDataContext.ApplicationApproveData
                    .Where(p => "车辆维修、保养申请表".Equals(p.WorkflowName!.Trim()) && inputReq.CurrentUser == p.ApproverName!.Trim())
                    .Select(p => p.ApplicationNumber!.Trim())
                    .Distinct()
                    .ToListAsync();
                newlist = newlist.Where(p => folioList.Contains(p.Folio!.Trim())).ToList();
            }

            var count = newlist.Count();
            var data = newlist.OrderByDescending(p => p.ApplicationDate)
                .Skip((inputReq.PageIndex - 1) * inputReq.PageSize)
                .Take(inputReq.PageSize).ToList();

            return (data, count);
        }

        /// <summary>
        /// 保存报表数据
        /// </summary>
        /// <param name="reportModel"></param>
        /// <returns></returns>
        public async Task<int> SaveReportData(VehicleDispatchReportDto reportModel)
        {
            dbContext.VehicleDispatchReport.AddOrUpdate(p => p.ProcInstId, reportModel);
            return await dbContext.SaveChangesAsync();
        }
    }
}
